import vue from "vue";

export default {
  //创建表方法
  createString(sqlTable, index) {
    return `CREATE TABLE IF NOT EXISTS ${sqlTable[index]}`;
  },
  //index --->1为搜索,其他不需要参数
  operationalDatabase(statements, index) {
    let to = JSON.stringify({
      Sqlstr: statements
    });
    return arguments[1]
      ? JSON.parse(vue.prototype.$shellSQL(to, index))
      : JSON.parse(vue.prototype.$shellSQL(to));
  },

  //修改文本内容发送给后台
  textMsg(myMsg) {
    let regLess = /(\<div\>\<br\>\<\/div\>)*$/g; //去除末尾无意义换行
    myMsg = myMsg.replace(regLess, "");
    let reg = /\<img src=['"]static(.+?)\>/g; //匹配图片
    var imgArr = myMsg.match(reg);
    var regx = /\d+\.gif/g; //匹配gif
    var a = [];
    var sendToAndior = myMsg;
    if (imgArr) {
      for (let i = 0, len = imgArr.length; i < len; i++) {
        let sdn = imgArr[i].match(regx);
        let ins = parseInt(sdn[0].replace(".gif", "")); //获取图片数字
        a.push(ins);
      }
      for (let i = 0, len = imgArr.length; i < len; i++) {
        //找到图片描述
        let m = Math.floor(a[i] / 36);
        let q = a[i] % 36;
        sendToAndior = sendToAndior.replace(
          imgArr[i],
          "[" + this.expression[m][q] + "]"
        );
      }
    }
    //找到非<br>div
    let regAdd = /\<div\>[^\<]*\<\/div\>/g;
    let addArr = Array.from(new Set(sendToAndior.match(regAdd)));
    for (let i in addArr) {
      let arrtest = addArr[i].replace(/-/g, "/-/");
      let regh = new RegExp(arrtest, "gim");
      let regDiv = /\<div\>|\<\/div\>/g;
      let str = addArr[i].replace(regDiv, "");
      sendToAndior = sendToAndior.replace(regh, str);
    }
    return sendToAndior;
  },

  createdUserInfo(sqlTable) {
    // 新增初始化用户信息列表
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "userInformation"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT, id CHAR(60) UNIQUE, name NVARCHAR(18), gender CHAR(2), head CHAR(60) NOT NULL DEFAULT"",account CHAR(20),jobtitle NVARCHAR(20) NOT NULL DEFAULT "",department NVARCHAR(20) NOT NULL DEFAULT "",isAdmin CHAR(2),radioOf CHAR(2),groupOf CHAR(2),remark NVARCHAR(50) NOT NULL DEFAULT "",nickname NVARCHAR(50) NOT NULL DEFAULT "");`
    );
  },

  // 新增用户同步记录时间表
  createdUserTime(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "updateTime"
      )}(id INTEGER PRIMARY KEY AUTOINCREMENT,
    account CHAR(20), updateTime CHAR(15) NOT NULL DEFAULT '');`
    );
  },

  createGroupUser(sqlTable) {
    return this.operationalDatabase(
      `CREATE TABLE IF NOT EXISTS ${
        sqlTable.groupUsers
      }(_id iNTEGER PRIMARY KEY AUTOINCREMENT,userId CHAR(60),position INTEGER,name CHAR(15),gender INTEGER,groupId CHAR(60),info CHAR(120) UNIQUE);`
    );
  },

  //广播表
  createBroad(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "broadcastList"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT ,broadSendId CHAR(60),value TEXT,title NVARCHAR(100),time CHAR(15),messageId CHAR(60) NOT NULL UNIQUE,isRead INTEGER NOT NULL ,isSuccessSend CHAR(10));`
    );
    this.operationalDatabase(
      `ALTER TABLE ${sqlTable.broadcastList} ADD attachsize NVARCHAR(100) NULL;`
    );
    this.operationalDatabase(
      `ALTER TABLE ${sqlTable.broadcastList} ADD attach NVARCHAR(100) NULL;`
    );
    this.operationalDatabase(
      `ALTER TABLE ${sqlTable.broadcastList} ADD attachName NVARCHAR(100) NULL;`
    );
    this.operationalDatabase(
      `ALTER TABLE ${sqlTable.broadcastList} ADD deptIds NVARCHAR(100) NULL;`
    );
    this.operationalDatabase(
      `ALTER TABLE ${
        sqlTable.broadcastList
      } ADD broadSendName NVARCHAR(100) NULL;`
    );
    this.operationalDatabase(
      `ALTER TABLE ${sqlTable.broadcastList} ADD fileMap NVARCHAR(300) NULL;`
    );
    this.operationalDatabase(
      `ALTER TABLE ${sqlTable.broadcastList} ADD aType CHAR(1) NULL;`
    );
  },

  //会话表
  createSession(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "sessionList"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT,userId CHAR(60) UNIQUE,isGroup INTEGER NOT NULL DEFAULT 0,time CHAR(15));`
    );
  },

  //免打扰表
  createUninterrupted(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "uninterruptedList"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT,userId CHAR(60) UNIQUE);`
    );
  },

  //公众号表
  createPublic(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "publicList"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT,userId CHAR(60) UNIQUE,userName NVARCHAR(18), value TEXT,time CHAR(15),messageId CHAR(60),isRead INTEGER NOT NULL,head CHAR(30));`
    );
  },

  //群表
  createGroup(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "groupList"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT,id CHAR(60) UNIQUE,name NVARCHAR(30),groupIntroduce NVARCHAR(80),isJoin INTEGER,head CHAR(60),total INTEGER);`
    );
  },

  //群消息表 审核表
  createGroupMessage(sqlTable) {
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "group_message"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT,applyid CHAR(60),groupid CHAR(60),userName NVARCHAR(60),sendName NVARCHAR(60),jobTitle CHAR(60),gender CHAR(10),groupName NVARCHAR(80),head CHAR(80),shareid CHAR(60),type CHAR(60),agreest CHAR(30),opinion CHAR(300),time CHAR(50),department CHAR(60),messageId CHAR(60),isRead INTEGER NOT NULL);`
    );
    this.operationalDatabase(
      `${this.createString(
        sqlTable,
        "audit_notice"
      )}(_id INTEGER PRIMARY KEY AUTOINCREMENT,applyid CHAR(60),groupid CHAR(60),userName NVARCHAR(60),sendName NVARCHAR(60),jobTitle CHAR(60),gender CHAR(10),groupName NVARCHAR(80),head CHAR(80),shareid CHAR(60),type CHAR(60),agreest CHAR(30),opinion CHAR(300),time CHAR(50),department CHAR(60),messageId CHAR(60),isRead INTEGER NOT NULL);`
    );
  },

  //我的文章表
  createArticleList(sqlTable) {
    this.operationalDatabase(`CREATE TABLE IF NOT EXISTS ${
      sqlTable.articleList
    } (_id INTEGER PRIMARY KEY autoincrement,
      sendName char,
      sendId char,
      time char,
      articleTitle char,
      articleUrl char,
      articleMessage char,
      articleEditor char,
      articleImgUrl char);`);

      this.operationalDatabase(`ALTER TABLE ${sqlTable.articleList} ADD "receiveUser" char [null]`)
  },

  //我的收藏表
  createCollectList(sqlTable) {
    this.operationalDatabase(`CREATE TABLE IF NOT EXISTS ${
      sqlTable.collectList
    } (_id INTEGER PRIMARY KEY autoincrement,
      CREATEDATE char,
      FILE_ID char,
      FILE_TYPE char,
      FROM_ID char,
      FROM_NAME char,
      ID char,
      ISDONE char,
      IS_REMIND char,
      MESSAGE_ID char,
      REMIND_TIME char,
      TYPE char,
      USER_ID char,
      VALUE char);`);
  },

  //用户头像表
  createHeadPathList(sqlTable, myInfo) {
    this.operationalDatabase(`CREATE TABLE IF NOT EXISTS ${
      sqlTable.headPathList
    } (_id INTEGER PRIMARY KEY autoincrement,
      id char,
      head char,
      headPath char);`);
    // 新增字段可以兼容上个版本
    let isHeadPathID = this.operationalDatabase(
      `select * from sqlite_master where name = '${
        sqlTable.headPathList
      }' and sql like '%headPathID%';`,
      1
    );
    if (isHeadPathID.list.length == 0) {
      this.operationalDatabase(
        `ALTER TABLE ${
          sqlTable.headPathList
        } ADD COLUMN headPathID CHAR(600)  NOT NULL DEFAULT "";`
      );
    }
    // this.downLoadHeadPath(this.myInfo, "isMy");//新增下载头像

    window.downLoadHeadPathUrl(myInfo); //新增下载头像ID匹配
  },

  // 文件信息表(目前包括我的待办公文下载的文件、共享文件下载的文件)
  createOfficialRemindList(sqlTable) {
    this.operationalDatabase(`CREATE TABLE IF NOT EXISTS ${
      sqlTable.officialRemindList
    } (_id INTEGER PRIMARY KEY autoincrement,
      FILE_SORECE char,FILE_ID char,FILE_NAME char,LOCAL_PATH char,LOAD_URL char,CREATE_TIME char);`);
  },

  //创建聊天记录表
  createMessage(sqlTable) {
    this.operationalDatabase(
      `CREATE TABLE IF NOT EXISTS ${
        sqlTable.messageList
      }(_id INTEGER PRIMARY KEY AUTOINCREMENT,name CHAR(60),receiveId CHAR(60),sendId CHAR(60),type CHAR(10),value TEXT,time CHAR(20),messageId CHAR(60) UNIQUE,isRead INTEGER NOT NULL,isSuccessSend CHAR(10),isDownLoad CHAR(60),DownLoadId NVARCHAR(80),duration CHAR(10));`
    );
    this.operationalDatabase(
      `CREATE TABLE IF NOT EXISTS ${
        sqlTable.privateList
      }(_id INTEGER PRIMARY KEY AUTOINCREMENT,name CHAR(60),receiveId CHAR(60),sendId CHAR(60),type CHAR(10),value TEXT,time CHAR(20),messageId CHAR(60) UNIQUE,isRead INTEGER NOT NULL,isSuccessSend CHAR(10),isDownLoad CHAR(60),DownLoadId NVARCHAR(80),duration CHAR(10));`
    );
    let insertField = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.privateList} ADD isByRead INTEGER NULL;`
    );
    let insertMessageField = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD readData TEXT NULL;`
    );
    let insertDownloadField = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD downloadData TEXT NULL;`
    ); //zk新增。下载详情字段
    let insertFont = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.privateList} ADD fontSize CHAR(80) NULL;`
    );
    let insertFont_g = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD fontSize CHAR(80) NULL;`
    );
    let insertService = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.privateList} ADD isServiceAcc INTEGER NULL;`
    ); //新增是否客服消息字段
    let insertService_g = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD isServiceAcc INTEGER NULL;`
    ); //新增是否客服消息字段
    //zk新增，群聊和私聊加入原图和小图字段
    let insertMessageSmallValue = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD smallValue TEXT NULL;`
    ); //群聊小图
    let insertMessageRawValue = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD rawValue TEXT NULL;`
    ); //群聊原图
    let insertPrivateSmallValue = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.privateList} ADD smallValue TEXT NULL;`
    ); //私聊小图
    let insertPrivateRawValue = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.privateList} ADD rawValue TEXT NULL;`
    ); //私聊原图

    let insertReplay = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.privateList} ADD replay CHAR(60) NULL;`
    );
    let insertReplay_g = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.messageList} ADD replay CHAR(60) NULL;`
    );
    // zk1225新增
    // 添加表的索引
    this.operationalDatabase(
      `CREATE INDEX privateList_IDX_MessageEnity_time_DESC ON ${
        sqlTable.privateList
      } ("time" DESC)`
    );
    this.operationalDatabase(
      `CREATE INDEX privateList_IDX_MessageEnity_receiveId_ASC ON ${
        sqlTable.privateList
      } ("receiveId" ASC)`
    );
    this.operationalDatabase(
      `CREATE INDEX privateList_IDX_MessageEnity_sendId_ASC ON ${
        sqlTable.privateList
      } ("sendId" ASC)`
    );
    this.operationalDatabase(
      `CREATE INDEX messageList_IDX_MessageEnity_time_DESC ON ${
        sqlTable.messageList
      } ("time" DESC)`
    );
    this.operationalDatabase(
      `CREATE INDEX messageList_IDX_MessageEnity_receiveId_ASC ON ${
        sqlTable.messageList
      } ("receiveId" ASC)`
    );
    this.operationalDatabase(
      `CREATE INDEX messageList_IDX_MessageEnity_sendId_ASC ON ${
        sqlTable.messageList
      } ("sendId" ASC)`
    );
  },
  //创建公众号记录报
  createPublickRecors(sqlTable) {
    this.operationalDatabase(
      `CREATE TABLE IF NOT EXISTS ${
        sqlTable.publicRecords
      }(_id INTEGER PRIMARY KEY AUTOINCREMENT,id CHAR(60),value TEXT,time CHAR(20),messageId CHAR(60),isRead INTEGER NOT NULL);`
    );
  },
  //热键表
  createHotKey(sqlTable) {
    this.operationalDatabase(
      `CREATE TABLE IF NOT EXISTS ${
        sqlTable.userHotKey
      }(_id INTEGER PRIMARY KEY AUTOINCREMENT,id CHAR(80) UNIQUE,search CHAR(15),screen CHAR(15),session CHAR(15),window CHAR(15),send CHAR(15));`
    );
    let insertPath = this.operationalDatabase(
      `ALTER TABLE ${sqlTable.userHotKey} ADD path CHAR(200) NULL;`
    );
  },

  allFun(sqlTable, myInfo) {
    Object.keys(this).forEach(i => {
      if (
        i != "createString" &&
        i != "operationalDatabase" &&
        i != "textMsg" &&
        i != "allFun"
      ) {
        
        this[i](sqlTable, myInfo);
      }
    });
  }
};
